package syuhei.mdatools;

import java.io.IOException;
import javax.servlet.http.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import syuhei.mdatools.PMF;
import java.util.Date;
import java.util.List;
import javax.jdo.PersistenceManager;

@SuppressWarnings("serial")
public class MdatoolsServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        if (user != null) {
            PersistenceManager pm = PMF.get().getPersistenceManager();
            javax.jdo.Query query = pm.newQuery(syuhei.mdatools.database.User.class);
            query.setFilter("name == nameParam");
            query.declareParameters("String nameParam");
            try {  
                List<syuhei.mdatools.database.User> results = (List<syuhei.mdatools.database.User>) query.execute(user.getNickname());
                if(results.isEmpty())
                {
                	syuhei.mdatools.database.User myuser = new syuhei.mdatools.database.User(user.getNickname(), 0);
                	pm.makePersistent(myuser);
                }
                else
                {
                }
            } finally {  
                query.closeAll();  
                pm.close();  
            }
        } else {
            resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
        }
	}
}
